let loginForm = document.getElementsByName('loginForm')[0];
let usernameInp = document.querySelector('#username')
let pwdInp = document.querySelector('#pwd')

import { loginReq } from '../../api'

import { setToken, setUser } from '../../utils/storageUtils'

import userAxios from '../../api/request'


// 表单提交事件
loginForm.onsubmit = async function(e) {

    // 取消浏览器的默认行为
    e.preventDefault();

    // console.log("提交");

    let username = usernameInp.value;
    let pwd = pwdInp.value;

    // loginReq(username, pwd).then(({ code, message, data: { token, user } }) => {
    //     // console.log(data);

    //     if (code == '2000') {
    //         // 存入token到本地存储
    //         setToken(token)
    //         setUser(user);

    //         window.location.href = '/users.html';
    //     } else {
    //         alert(message)
    //     }
    // }, rea => {
    //     console.log(rea);
    // })

    try {
        let { code, message, data: { token, user } } = await loginReq(username, pwd);
        // console.log(data);
        if (code == '2000') {
            // 存入token到本地存储
            setToken(token)
            setUser(user);

            window.location.href = '/users.html';
        } else {
            alert(message)
        }
    } catch (err) {
        alert(err);
    }

}